﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyContextMenuStrip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

    

        private void listBox1_MouseUp (object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = nihao.IndexFromPoint(e.Location);
                if (index >= 0)
                {
                    nihao.SetSelected(index, true);
                    addToolStripMenuItem.Enabled = true;
                    deleteToolStripMenuItem.Enabled = true;
                }
                else // index 为 -1 
                {
                    //禁用右键菜单
                    nihao.ClearSelected();
                    addToolStripMenuItem.Enabled = false;
                    deleteToolStripMenuItem.Enabled = false;
                }
                this.contextMenuStrip1.Show(nihao, e.Location);
            }
        }



        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("add sucess");
        }
    }
}
